<?php
namespace app\shop\controller\admin;

use app\admin\library\AdminController;
use app\shop\library\ApiController;
use app\shop\library\coupon\Coupon as C;
class Coupon extends AdminController{

    public function onIndex(){

        $this->pageTitle = '优惠券';
        $this->pageIcon = 'fa-ticket';
        return $this->display();
    }


    public function onList(){
        $keywords = $this->post('keywords',$this->check()->type('string'));
        $page = $this->post('page_current',$this->check()->type('int')->default(1));
        $pagesize =  30;
        $group_id = $this->post('group_id',$this->check()->type('int'));
        $status   = $this->post('status',$this->check()->type('string'));
        $Coupon =  $this->model('Coupon');
        if ($keywords){
            $Coupon->where('title','like','%'.$keywords.'%');
        }
        if ($group_id){
            $Coupon->where('group_id',$group_id);
        }
        if ($status){
            $Coupon->where('status',$status);
        }
        $Coupon->order('coupon_id','desc');
        $list = $Coupon->page($page,$pagesize)->getData();
        return ['list'=>$list,'total'=>$Coupon->total,'pageCount'=>$Coupon->pageCount];
    }



    public function onEdit(){
        $id = $this->get('id',$this->check()->type('int'));

        if ($id){
            $Coupon =  $this->model('Coupon');
            $data = $Coupon->getId($id);
            $this->set('coupon',$data);
            $this->pageTitle = '编辑优惠券';
        }else{
            $this->pageTitle = '添加优惠券';
        }

        $CouponGroup =  $this->model('CouponGroup');
        $groupList = $CouponGroup->where('cold',0)->order('sort','desc')->getData();

        $option = [];

        foreach ($groupList as $k=>$v){
            $option[$v['group_id']] = $v['title'];
        }

        $this->set('group',$option);
        
        return $this->display();
    }


    public function onSave(){
        $id = $this->post('coupon_id',$this->check()->type('int'));

        $data['group_id'] = $this->post('group_id',$this->check()->type('int')->required());
        $data['type'] = $this->post('type',$this->check()->type('string')->required());
        $data['desc'] = $this->post('desc',$this->check()->type('string'));
        $data['price'] = $this->post('price',$this->check()->type('float')->default(0));
        $data['condition_price'] = $this->post('condition_price',$this->check()->type('float')->default(0));
        $data['stock'] = $this->post('stock',$this->check()->type('int')->default(0));
        $data['starttime'] = $this->post('starttime',$this->check()->type('string')->default(''));
        $data['endtime'] = $this->post('endtime',$this->check()->type('string')->default(''));
        $data['member_receive_number'] = $this->post('member_receive_number',$this->check()->type('int')->default(1));
        $updated_at = $this->post('updated_at',$this->check()->type('string')->default(''));

        $data['apply_type'] = $this->post('apply_type',$this->check()->type('string')->default('all'));
        $data['apply_goods_list'] = $this->post('apply_goods_list',$this->check()->type('string')->default(''));
        $data['apply_cate_list'] = $this->post('apply_cate_list',$this->check()->type('string')->default(''));

        $Coupon =  $this->model('Coupon');
        if ($id){
            $oldData = $Coupon->getId($id);
            if (!$oldData){
                throw new \Exception('数据不存在');
            }

            if ($oldData['updated_at'] != $updated_at){
                throw new \Exception('数据已修改，请刷新页面后重试');
            }

            if ($oldData['status']=='sale'){
                throw new \Exception('优惠券正在使用中，请先停止使用');
            }

            $Coupon->updateId($id,$data);
            $this->log('修改优惠券',$data);
        }else{
            $Coupon->insert($data);
            $this->log('添加优惠券',$data);
        }

        return true;
    }


    public function onStatus(){

        $status = $this->post('status',$this->check()->type('string')->required());
        $id = $this->post('coupon_id',$this->check()->type('int')->required());

        $Coupon =  $this->model('Coupon');

        if ($Coupon->updateId($id,['status'=>$status])){
            $this->log('修改优惠券状态',['status'=>$status]);
            C::clearCache($id);
            return true;
        }

        throw new \Exception('修改失败');
    }




    public function onGroupList(){
        $GoodsTagGroup =  $this->model('CouponGroup');
        return $GoodsTagGroup->order('sort','desc')->getData();
    }

   
    public function onSaveGroup(){
        $data['title'] = $this->post('title',$this->check()->type('string')->required());

        $id  =  $this->post('group_id',$this->check()->type('int'));

        $GoodsTagGroup =  $this->model('CouponGroup');

        if ($id){
            $GoodsTagGroup->updateId($id,$data);
            $this->log('修改标签组',$data);
        }else{
            $data['sort'] = $GoodsTagGroup->max('sort')+1;
            $GoodsTagGroup->insert($data);
            $this->log('添加标签组',$data);
        }

        return true;
    }

    public function onGroupCold(){
        
        $id =  $this->post('id',$this->check()->type('int')->required());
        $cold = $this->post('cold',$this->check()->type('int')->default(0));
        $GoodsTagGroup =  $this->model('CouponGroup');
        if ($GoodsTagGroup->updateId($id,['cold'=>$cold])){
            
            return true;
        }

        throw new \Exception('修改失败');
    }

    public function onDelGroup(){
        $id = $this->post('group_id',$this->check()->type('int')->required());
        $GoodsTagGroup =  $this->model('CouponGroup');
        $oldData = $GoodsTagGroup->getId($id);
        if (!$oldData){
            throw new \Exception('数据不存在');
        }

        
        if ($GoodsTagGroup->deleteId($id)){
            return true;
        }
    }

    public function onGroupSort(){
        $ids = $this->post('ids',$this->check()->type('array')->required());

        if (count($ids)<2){
            throw new \Exception('排序失败');
        }

        $GoodsTagGroup =  $this->model('CouponGroup');

        $GoodsTagGroup->switchSort($ids,'sort','desc');

        return true;
    }

    public function onApplyGoodsInit(){
        $coupon_id = $this->post('coupon_id',$this->check()->type('int')->required());

        $Coupon =  $this->model('Coupon');

        $data = $Coupon->getId($coupon_id);
        $re  = [];
        if ($data['apply_goods_list']!=''){
            $re[]= ['value'=>'goods','label'=>'商品'];
        }

        if ($data['apply_cate_list']!=''){
            $cate = $this->model('Cate');
            $list = $cate->where('cate_id','in',$data['apply_cate_list'])->getData();
            foreach ($list as $k=>$v){
                $re[]= ['value'=>$v['cate_id'],'label'=>'[分类]'.$v['title']];
            }
        }

        return $re;
    }


    public function onSend(){
        $coupon_id = $this->post('id',$this->check()->type('int')->required());
        $sendType = $this->post('sendType',$this->check()->type('string')->required());
        $sendMember = $this->post('sendMember',$this->check()->type('array')->required());

        $Coupon =  $this->model('Coupon');

        $data = $Coupon->getId($coupon_id);

        if (!$data){
            throw new \Exception('数据不存在');
        }       
    }
}